import { ForgetPwd, SendUserCode } from "@/api";

let codeMaxSecond = 0;
let codeInterval = null;

const CheckPassword = (password, againPassword) => {
    if (isEmpty(password)) {
        Message().warning("请输入密码");
        return false;
    } else if (password != againPassword) {
        Message().warning("两次输入的密码不一致");
        return false;
    }

    return true;
};

export const useContent = () => {
    const userStore = useUserStore();
    const pageStore = usePageStore();
    const phone = ref("");
    const code = ref("");
    const password = ref("");
    const againPassword = ref("");
    const codeText = ref("获取验证码");

    const onSendCodeSuccess = () => {
        const _func = () => {
            if (codeMaxSecond === 0) {
                codeText.value = `获取验证码`;
                clearInterval(codeInterval);
            } else {
                codeText.value = `${codeMaxSecond--}s`;
            }
        };
        codeMaxSecond = 120;
        codeInterval = setInterval(_func, 1000);
        _func();
    };

    const onSendCode = async () => {
        if (codeMaxSecond > 0) return;
        if (isPhone(phone.value)) {
            const res = await SendUserCode({
                phone: phone.value,
                event: "forgetpwd",
            });

            if (res.code == 200) {
                Message().success("发送成功");
                onSendCodeSuccess();
            } else {
                Message().error(res.msg);
            }
        } else {
            Message().warning("请输入正确的手机号");
        }
    };

    const onToLogin = () => {
        navigateTo("/login.html");
    };

    const onSubmit = async () => {
        if (isEmpty(phone.value)) {
            Message().warning("请输入手机号");
            return false;
        }

        if (!isPhone(phone.value)) {
            Message().warning("请输入正确的手机号");
            return false;
        }

        if (isEmpty(code.value)) {
            Message().warning("请输入验证码");
            return false;
        }

        if (!CheckPassword(password.value, againPassword.value)) {
            return false;
        }

        const res = await ForgetPwd({
            phone: phone.value,
            new_password: password.value,
            confirm_password: againPassword.value,
            code: code.value,
        });

        if (res.code == 200) {
            userStore.CLEAR_USERINFO();
            if (pageStore.isMobile) {
                Message().success({
                    content: "修改成功",
                    duration: 1500,
                    onClosed() {
                        navigateTo("/login.html");
                    },
                });
            } else {
                Message().success({
                    message: "修改成功",
                    duration: 1500,
                    onClose() {
                        navigateTo("/login.html");
                    },
                });
            }
        } else {
            Message().error(res.msg);
        }
    };

    return {
        phone,
        code,
        password,
        againPassword,
        codeText,

        onSendCode,
        onToLogin,
        onSubmit,
    };
};
